﻿using MyTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Collection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 关联性泛型集合(key-Value)

        #region Dictionary(哈希表)
        Dictionary<string, Product2> dictProducts = new Dictionary<string, Product2>();
        dictProducts.Add("t1", new Product2());
        dictProducts.Add("t2", new Product2());
        dictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小:" + dictProducts.Count + "<br>");
        Product2 p;
        Response.Write(dictProducts.TryGetValue("t4",out p)+"<br>");
        StringBuilder sb = new StringBuilder();
        foreach(var key in dictProducts.Keys)
        {
            sb.Append(key + "\t");
        }
        Response.Write(sb.ToString() + "<br>");
        #endregion

        #region SortedDictionary(二叉树)
        SortedDictionary<string, Product2> sDictProducts = new SortedDictionary<string, Product2>();
        sDictProducts.Add("t1", new Product2());
        sDictProducts.Add("t4", new Product2());
        sDictProducts.Add("t2", new Product2());
        sDictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小:" + sDictProducts.Count + "<br>");
        Response.Write(sDictProducts.ContainsKey("t10")+"<br>");
        StringBuilder sb2 = new StringBuilder();
        foreach(var key in sDictProducts.Keys)
        {
            sb2.Append(key + "\t");
        }
        Response.Write(sb2.ToString() + "<br>");
        #endregion

        #region SortedList(数组)
        SortedList<string, Product2> sListProducts = new SortedList<string, Product2>();
        sListProducts.Add("t3", new Product2());
        sListProducts.Add("t2", new Product2());
        sListProducts.Add("t1", new Product2());
        StringBuilder sb3 = new StringBuilder();
        foreach(var key in sListProducts.Keys)
        {
            sb3.Append(key + "\t");
        }
        Response.Write(sb3.ToString() + "<br>");
        #endregion

        #endregion

        #region 非关联性泛型集合

        #region List(数组)
        List<Product2> lproducts = new List<Product2>();
        lproducts.Add(new Product2());
        Response.Write("集合元素个数:" + lproducts.Count + "<br>");
        Response.Write("第一个元素的hash值:" + lproducts[0].GetHashCode()+"<br>");
        #endregion

        #region LinkedList(双向链表)
        LinkedList<Product2> llproducts = new LinkedList<Product2>();
        llproducts.AddLast(new Product2("aa", "type"));
        llproducts.AddFirst(new Product2("bb", "type"));
        StringBuilder sb4 = new StringBuilder();
        foreach(var product in llproducts)
        {
            sb4.Append(product.Name + "\t");
        }
        Response.Write(sb4.ToString() + "<br>");
        #endregion

        #region HashSet(哈希表) "相同的对象:判断两个对象相等"
        HashSet<Product2> hsProducts = new HashSet<Product2>();
        hsProducts.Add(new Product2("cc", "type"));
        hsProducts.Add(new Product2("aa", "type"));
        hsProducts.Add(new Product2("aa", "type1"));
        hsProducts.Add(new Product2("bb", "type"));
        StringBuilder sb5 = new StringBuilder();
        foreach(var product in hsProducts)
        {
            sb5.Append(product.Name + "\t");
        }
        Response.Write(sb5.ToString() + "<br>S");

        #endregion

        #region SortedSet(二叉树)
        SortedSet<Product2> ssProducts = new SortedSet<Product2>();
        ssProducts.Add(new Product2("cc", "type"));
        ssProducts.Add(new Product2("aa", "type"));
        ssProducts.Add(new Product2("aa", "type"));
        ssProducts.Add(new Product2("bb", "type"));
        StringBuilder sb6 = new StringBuilder();
        foreach (var product in ssProducts)
        {
            sb6.Append(product.Name + "\t");
        }
        Response.Write(sb6.ToString() + "<br>");
        #endregion

        #region Stack(栈)
        Stack<Product2> sProducts = new Stack<Product2>();
        sProducts.Push(new Product2("aa", "type"));
        sProducts.Push(new Product2("bb", "type"));
        Response.Write("栈顶元素:" + sProducts.Peek().Name + "<br>");
        Response.Write("栈内元素个数:" + sProducts.Count + "<br>");
        Response.Write("出栈:" + sProducts.Pop().Name + "<br>");
        Response.Write("栈顶元素:" + sProducts.Peek().Name + "<br>");
        Response.Write("栈内元素个数:" + sProducts.Count + "<br>");

        #endregion

        #region Queue(队列)
        Queue<Product2> qProducts = new Queue<Product2>();
        qProducts.Enqueue(new Product2("aa", "type"));
        qProducts.Enqueue(new Product2("bb", "type"));
        Response.Write("队列头部元素:" + qProducts.Peek().Name + "<br>");
        Response.Write("队列元素个数:" + qProducts.Count + "<br>");
        Response.Write("队列出队元素:" + qProducts.Dequeue().Name + "<br>");
        Response.Write("<br>");
        Response.Write("队列头部元素:" + qProducts.Peek().Name + "<br>");
        Response.Write("队列元素个数:" + qProducts.Count + "<br>");

        #endregion
        #endregion

    }
}